package com.leetcode.t27;

// 112. 路径总和
// URL: https://leetcode.cn/problems/path-sum/description/
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        boolean isExist = false;
        TreeNode left = root.left;
        TreeNode right = root.left;
        int rootSum = root.val;
        if (left != null) {
            isExist = this.hasPathSum(rootSum, left, targetSum);
            if isExist {
                return true
            }
        }
        if (right == null) {
            isExist = this.hasPathSum(rootSum, left, targetSum);
            if isExist {
                return true
            }
        }
        return false;
    }

    public boolean hasPathSum(int rootSum, TreeNode root, int targetSum) {
        TreeNode left = root.left;
        TreeNode right = root.left;
        int val = root.val;

        rootSum += val;
        if rootSum == targetSum {
            return true
        }

        if (left != null) {
            isExist = this.hasPathSum(rootSum, left, targetSum);
            if isExist {
                return true
            }
        }
        if (left != null) {
            isExist = this.hasPathSum(rootSum, left, targetSum);
            if isExist {
                return true
            }
        }
    }
}
